
import { validatenull } from '@/utils/validate'

const StoreKey: string =  'zjc_webapp_';

export function getToken() {
  let userInfo = getStore({ name: '__QIANKUN_WEBAPP_MAIN_USERINFO__' })
  console.log('---userInfo---', userInfo);
  if (userInfo && userInfo.token) {
    return userInfo.token;
  }
  return null;
}

/**
 * 设置本地存储
 */

export const setStore = (params = {}) => {
  let { name, content, type } = params;
  name = StoreKey + name;
  if (type) {
      window.sessionStorage.setItem(name, JSON.stringify(content));
  } else {
      window.localStorage.setItem(name, JSON.stringify(content));
  }
};

/**
 * 获取本地存储
 */

export const getStore = (params:any = {}) => {
  let { name, debug } = params
  if (name !== '__QIANKUN_WEBAPP_MAIN_USERINFO__') {
    name = StoreKey + name
  }
  console.log('getStoresdsdsdsd', name);
  let obj: any = null
  obj = window.sessionStorage.getItem(name)
  console.log('__QIANKUN_WEBAPP_MAIN_USERINFO__', validatenull(obj));
  if (validatenull(obj)) obj = window.localStorage.getItem(name)

  if (validatenull(obj)) return
  try {
    obj = JSON.parse(obj)
  } catch {
    return obj
  }
  if (debug) {
    return obj
  }
  return obj;
};
/**
 * 删除本地存储
 */
export const removeStore = (params: any) => {
  let { name, type } = params;
  if (name !== '__QIANKUN_WEBAPP_MAIN_USERINFO__') {
    name = StoreKey + name
  }
  if (type) {
    window.sessionStorage.removeItem(name);
  } else {
    window.localStorage.removeItem(name);
  }
};

/**
 * 清空全部本地存储
 */

export const clearStore = (params: any = {}) => {
  let { type } = params;
  if (type) {
    window.sessionStorage.clear();
  } else {
    window.localStorage.clear();
    window.sessionStorage.clear();
  }
};
